#ifndef _myDistance_H_
#define _myDistance_H_

#include <VL53L1X.h>

#define VL53L1X_ADDRESS 0x29 // 位移传感器地址

// 位移传感器实例
VL53L1X DistanceSensor;

/// @brief 位移传感器初始化
bool DistanceInitialize()
{
    if (VL53L1X_ADDRESS != DistanceSensor.getAddress())
    {
        DistanceSensor.setAddress(VL53L1X_ADDRESS);
    }
    DistanceSensor.setTimeout(50);
    if (!DistanceSensor.init())
    {
        Serial.println("Failed to detect and initialize sensor!");
        return false;
    }
    // Use long distance mode and allow up to 50000 us (50 ms) for a measurement.
    // You can change these settings to adjust the performance of the sensor, but
    // the minimum timing budget is 20 ms for short distance mode and 33 ms for
    // medium and long distance modes. See the VL53L1X datasheet for more
    // information on range and timing limits.
    DistanceSensor.setDistanceMode(VL53L1X::Long);
    DistanceSensor.setMeasurementTimingBudget(20000);
    // Start continuous readings at a rate of one measurement every 50 ms (the
    // inter-measurement period). This period should be at least as long as the
    // timing budget.
    DistanceSensor.startContinuous(50);
    return true;
}

/// @brief 获取位移传感器数据
uint16_t GetDistance()
{
    return DistanceSensor.read();

    //  //Model ID查询，返回0XEA
    //   uint16_t ad=0x010f;
    //   Wire.beginTransmission(VL53L1X_ADDRESS);
    //   Wire.write(ad>>8);
    //   Wire.write(ad);
    //   Wire.endTransmission(false);
    //   Wire.requestFrom(VL53L1X_ADDRESS, 1);
    //   byte data = Wire.read();
    //   Serial.print("Model ID: 0x");
    //   Serial.println(data,HEX);
    //   Serial.println("Distance end...");

    // //Module Type查询，返回0XCC
    // uint16_t ad=0x0110;
    // Wire.beginTransmission(VL53L1X_ADDRESS);
    // Wire.write(ad>>8);
    // Wire.write(ad);
    // Wire.endTransmission(false);
    // Wire.requestFrom(VL53L1X_ADDRESS, 1);
    // byte data = Wire.read();
    // Serial.print("Module Type: 0x");
    // Serial.println(data,HEX);
    // Serial.println("Distance end...");

    // //Mask Revision询，返回0X10
    // uint16_t ad=0x0111;
    // Wire.beginTransmission(VL53L1X_ADDRESS);
    // Wire.write(ad>>8);
    // Wire.write(ad);
    // Wire.endTransmission(false);
    // Wire.requestFrom(VL53L1X_ADDRESS, 1);
    // byte data = Wire.read();
    // Serial.print("Mask Revision: 0x");
    // Serial.println(data,HEX);
    // Serial.println("Distance end...");

    //   //地址询，返回实际地址，当前返回0x29
    //   uint16_t ad=VL53L1X::regAddr::I2C_SLAVE__DEVICE_ADDRESS;
    //   Wire.beginTransmission(VL53L1X_ADDRESS);
    //   Wire.write(ad>>8);
    //   Wire.write(ad);
    //   Wire.endTransmission(false);
    //   Wire.requestFrom(VL53L1X_ADDRESS, 1);
    //   byte data = Wire.read();
    //   Serial.print("IIC Address: 0x");
    //   Serial.println(data,HEX);
    //   Serial.println("Distance end...");
}

/// @brief 获取距离值
void ShowDistanceValue()
{
  Serial.println("Distance start...");
  Serial.print("Distance:");
  Serial.print(GetDistance());
  Serial.println("mm");
  Serial.println("Distance end...");
}
#endif